﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jingxing.Models.Home;

namespace Jingxing.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            load(model);

            ORDesignerDataContext context = new ORDesignerDataContext();
            model.BannerList = (from u in context.PageElement
                          where u.Type.Trim() == "Banner"
                          orderby u.Seq
                          select new Jingxing.Models.Admin.Banner()
                          {
                              ID = u.Id,
                              Name = u.Content,
                              ImageLink = u.ImageLink,
                              Link = u.Link,
                              Seq = u.Seq
                          }).ToList();

            model.公司简介List = (from a in context.Article
                              where a.Type == "Introduction"
                                  orderby a.Seq
                                  select a).Take(4).ToList();

            model.新闻中心List = (from a in context.Article
                              where a.Type == "News"
                              orderby a.Seq
                              select a).Take(4).ToList();

            model.加盟连锁List = (from a in context.Article
                              where a.Type == "ChainStore"
                              orderby a.Seq
                              select a).Take(4).ToList();

            model.产品项目List = (from a in context.Article
                              where a.Type == "Product"
                              orderby a.Seq
                              select a).Take(4).ToList();

            model.培训学校List = (from a in context.Article
                              where a.Type == "School"
                              orderby a.Seq
                              select a).Take(4).ToList();

            model.直营店List = (from a in context.Article
                              where a.Type == "Store"
                              orderby a.Seq
                              select a).Take(4).ToList();

            return View(model);
        }

        private void load(LayoutModel model)
        {
            ORDesignerDataContext context = new ORDesignerDataContext();
            SystemData systemData = (from s in context.SystemData
                                     where s.Name.Trim() == "Footer"
                                     select s).SingleOrDefault();
            if (systemData == null)
                model.Footer = string.Empty;
            else
                model.Footer = systemData.Value.Trim();
        }

        public ActionResult TitleType(string type)
        {
            ORDesignerDataContext context = new ORDesignerDataContext();

            TitleTypeModel model = new TitleTypeModel();
            load(model);
            model.Type = type;

            model.List = (from a in context.Article
                          where a.Type == type
                          orderby a.Seq
                          select a).ToList();
            switch (type)
            {
                case "Introduction":
                    model.Name = "公司简介";
                    break;
                case "News":
                    model.Name = "新闻中心";
                    break;
                case "ChainStore":
                    model.Name = "加盟连锁";
                    break;
                case "Product":
                    model.Name = "产品项目";
                    break;
                case "School":
                    model.Name = "汽车美容";
                    break;
                case "Store":
                    model.Name = "直营店";
                    break;
                case "Club":
                    model.Name = "俱乐部";
                    break;
                case "Contact":
                    model.Name = "联系我们";
                    break;
                case "Knowledge":
                    model.Name = "养护常识";
                    break;
            }

            return View(model);
        }

        public ActionResult Title(int id)
        {
            ORDesignerDataContext context = new ORDesignerDataContext();

            TitleModel model = new TitleModel();
            load(model);

            var article = (from a in context.Article
                           where a.Id == id
                           select a).SingleOrDefault();

            if(article == null)
                return RedirectToAction("Message", new { message = "没有找到文章！" });

            model.Title = article.Title;
            model.Content = article.Content;
            model.DateTime = article.DateTime;

            return View(model);
        }

        public ActionResult Comment()
        {
            CommentModel model = new CommentModel();
            load(model);
            return View(model);
        }

        [HttpPost]
        public ActionResult Comment(CommentModel model)
        {
            if (ModelState.IsValid)
            {
                ORDesignerDataContext context = new ORDesignerDataContext();
                Comment comment = new Comment();
                comment.Name = model.Name;
                comment.Contact = model.Contact;
                comment.Content = model.Content;
                comment.DateTime = DateTime.Now;

                context.Comment.InsertOnSubmit(comment);
                context.SubmitChanges();

                return RedirectToAction("Message", new { message = "留言成功" });
            }
            return View(model);
        }

        public ActionResult Message(string message)
        {
            MessageModel model = new MessageModel();
            load(model);
            model.Message = message;

            return View(model);
        }
    }
}
